package zhihui.mobile.ui.shop.coupon;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zgm.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.sales.CouponInfo;


/**
 * Created by 张公铭 on 2017/9/6 0006.
 */
public class CouponAdapter extends BaseAdapter
{
    private Context mContext;
    private LayoutInflater inflater;
    private List<CouponInfo> mList = new ArrayList<>();

    public CouponAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setList(List<CouponInfo> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
    }

    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return mList.get(position).getCouponId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LinearLayout ll_root;
        TextView tv_icon, tv_money, tv_desc, tv_title, tv_time, tv_state;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.shop_coupon_item, null);
            ll_root = (LinearLayout) convertView.findViewById(R.id.ll_coupon_item_root);
            tv_icon = (TextView) convertView.findViewById(R.id.tv_shop_coupon_item_icon);
            tv_money = (TextView) convertView.findViewById(R.id.tv_shop_coupon_item_money);
            tv_desc = (TextView) convertView.findViewById(R.id.tv_shop_coupon_item_desc);
            tv_title = (TextView) convertView.findViewById(R.id.tv_shop_coupon_item_title);
            tv_time = (TextView) convertView.findViewById(R.id.tv_shop_coupon_item_time);
            tv_state = (TextView) convertView.findViewById(R.id.tv_shop_coupon_item_use);

            ViewCache viewCache = new ViewCache();
            viewCache.ll_root = ll_root;
            viewCache.tv_icon = tv_icon;
            viewCache.tv_money = tv_money;
            viewCache.tv_desc = tv_desc;
            viewCache.tv_title = tv_title;
            viewCache.tv_time = tv_time;
            viewCache.tv_state = tv_state;

            convertView.setTag(viewCache);
        } else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            ll_root = viewCache.ll_root;
            tv_icon = viewCache.tv_icon;
            tv_money = viewCache.tv_money;
            tv_desc = viewCache.tv_desc;
            tv_title = viewCache.tv_title;
            tv_time = viewCache.tv_time;
            tv_state = viewCache.tv_state;
        }

        CouponInfo couponInfo = mList.get(position);
        ll_root.setBackgroundResource(R.drawable.coupon_bk);

        switch (couponInfo.getType())
        {
            case 1:
                tv_money.setText(((double) couponInfo.getCoupon() / 100) + "");
                tv_desc.setText("满" + ((double) couponInfo.getLimit() / 100) + "可用");
                tv_title.setText(couponInfo.getDescription());
                tv_time.setText(DateUtils.formatTime(couponInfo.getStarttime_f()) + "-" + DateUtils.formatTime(couponInfo.getEndtime_f()));
                break;
            case 2:
                tv_money.setText(couponInfo.getDiscount() + "折");
                if (couponInfo.getLimit() > 0)
                {
                    tv_desc.setText("满" + ((double) couponInfo.getLimit() / 100) + "可用");
                } else
                {
                    tv_desc.setVisibility(View.GONE);
                }
                tv_title.setText(couponInfo.getDescription());
                tv_time.setText(DateUtils.formatTime(couponInfo.getStarttime_f()) + "-" + DateUtils.formatTime(couponInfo.getEndtime_f()));
                break;
            case 3:
                String[] formulas = couponInfo.getFormula().split(":");
                tv_title.setText(couponInfo.getDescription());
                tv_time.setText(DateUtils.formatTime(couponInfo.getStarttime_f()) + "-" + DateUtils.formatTime(couponInfo.getEndtime_f()));
                if (formulas[1].equals("0"))
                {
                    tv_icon.setVisibility(View.GONE);
                    tv_money.setText("抵扣券");
                    tv_desc.setText("指定商品可用");
                } else
                {
                    tv_money.setText(((double) couponInfo.getCoupon() / 100) + "");
                    tv_desc.setVisibility(View.GONE);
                }
                break;
            case 4:
                tv_money.setText((couponInfo.getDiscount()/100) + "");
                tv_desc.setVisibility(View.GONE);
                tv_title.setText(couponInfo.getDescription());
                tv_time.setText("仅包裹服务可用");
                break;
        }

        switch (couponInfo.getStatus())
        {
            case 1:
                tv_state.setText("立即使用");
                break;
            case 2:
                tv_state.setText("已使用");
                break;
            case 3:
                tv_state.setText("已过期");
                ll_root.setBackgroundResource(R.drawable.coupon_timeout_bk);
                break;
        }

        return convertView;
    }

    class ViewCache
    {
        public LinearLayout ll_root;
        public TextView tv_icon, tv_money, tv_desc, tv_title, tv_time, tv_state;
    }
}
